package com.geovis.emergency.spd.controller.basic;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.controller.BaseController;
import com.geovis.common.core.entity.BaseEntity;
import com.geovis.emergency.spd.biz.basic.service.IBasicLabelGroupService;
import com.geovis.emergency.spd.biz.basic.service.IBasicLabelService;
import com.geovis.emergency.spd.biz.basic.wrapper.BasicLabelGroupWrapper;
import com.geovis.emergency.spd.entity.basic.entity.BasicLabel;
import com.geovis.emergency.spd.entity.basic.entity.BasicLabelGroup;
import com.geovis.emergency.spd.entity.basic.pojo.vo.BasicLabelGroupQueryVO;
import com.geovis.emergency.spd.entity.basic.pojo.vo.BasicLabelGroupVO;
import com.geovis.emergency.spd.entity.pc.pojo.enums.StatusEnum;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;

/**
 * @author Lonsho
 * @version 1.0
 * @date 2022/3/16
 */
@RestController
@AllArgsConstructor
@RequestMapping("/basic/label/group")
@Api(value = "基础模块-标签分组管理", tags = "标签分组管理")
public class BasicLabelGroupController extends BaseController<IBasicLabelGroupService> {
    private final IBasicLabelService basicLabelService;

    @GetMapping("/list")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "分组列表")
    public Result<List<BasicLabelGroupVO>> list(BasicLabelGroupQueryVO basicLabelGroup) {
        String name = basicLabelGroup.getName();
        basicLabelGroup.setName(null);
        LambdaQueryWrapper<BasicLabelGroup> wrapper = Wrappers.lambdaQuery(basicLabelGroup);
        if (StrUtil.isNotBlank(name)) {
            wrapper.like(BasicLabelGroup::getName, name);
        }
        wrapper.orderByAsc(BasicLabelGroup::getName);
        List<BasicLabelGroup> list = baseService.list(wrapper);
        BasicLabelGroup unGroup = new BasicLabelGroup("未分组");
        unGroup.setId("0");
        list.add(0, unGroup);
        if (!basicLabelGroup.isIgnoreAll()) {
            BasicLabelGroup allGroup = new BasicLabelGroup("所有标签");
            list.add(0,allGroup);
        }
        return Result.ok(BasicLabelGroupWrapper.build(basicLabelService).listVO(list));
    }

    @PostMapping("/save")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "新增分组", notes = "传入basicLabelGroup")
    public Result save(@RequestBody @Valid BasicLabelGroup basicLabelGroup) {
        LambdaQueryWrapper<BasicLabelGroup> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BasicLabelGroup::getName, basicLabelGroup.getName());
        if (baseService.count(wrapper) > 0) {
            return Result.failed(StrUtil.format("【{}】分组名已经存在了，请勿重复创建", basicLabelGroup.getName()));
        }
        return Result.ok(baseService.save(basicLabelGroup));
    }

    @PostMapping("/update")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "更新分组", notes = "传入basicLabelGroup")
    public Result update(@RequestBody @Valid BasicLabelGroup basicLabelGroup) {
        LambdaQueryWrapper<BasicLabelGroup> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BasicLabelGroup::getName, basicLabelGroup.getName());
        wrapper.ne(BasicLabelGroup::getId, basicLabelGroup.getId());
        if (baseService.count(wrapper) > 0) {
            return Result.failed(StrUtil.format("【{}】分组名已经存在了，请勿重复创建", basicLabelGroup.getName()));
        }
        return Result.ok(baseService.updateById(basicLabelGroup));
    }

    @PostMapping("/removeByIdList")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "删除", notes = "传入ids")
    public Result remove(@ApiParam(value = "主键集合", required = true) @RequestBody Set<String> idList) {
        boolean remove = baseService.removeBatchByIds(idList);
        if (remove) {
            //如果分组删除，将已经添加到分组的标签改成未分组
            LambdaUpdateWrapper<BasicLabel> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.in(BasicLabel::getGroupId, idList);
            updateWrapper.set(BasicLabel::getGroupId, StatusEnum.FALSE.getCode());
            basicLabelService.update(updateWrapper);
        }
        return Result.ok();
    }
}
